package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	public static void main(String[] args) throws IOException {
		//绑定端口
		ServerSocket ss = new ServerSocket(8080);
		System.out.println("我是服务器。。。");
		//监听,获取socket实例
		Socket socket = ss.accept();
		
		//获取输入流，并读取客户端信息
		InputStream is = socket.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		StringBuilder sb = new StringBuilder();
		String line = null;
		
		while((line = br.readLine())!=null) {
			sb.append(line);
		}
		System.out.println(sb.toString());
		socket.shutdownInput();
		
		//相应信息
		OutputStream os = socket.getOutputStream();
		PrintWriter pw = new PrintWriter(os);
		pw.write("欢迎您！！！");
		pw.flush();
		socket.shutdownOutput();
		
		//关闭资源
		pw.close();
		os.close();
		br.close();
		isr.close();
		is.close();
		socket.close();
		ss.close();
		
	}

}
